#最基础的模板
import requests

# 定义要爬取的 URL
url = 'https://tieba.baidu.com/f?kw=%E5%90%8A%E5%9B%BE&fr=index'

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
}

# 修改这一行，让 response 为 Response 对象
response = requests.get(url=url, headers=headers)
# 检查请求是否成功
if response.status_code == 200:
    filename = r"./page.html"
    # 以二进制写入模式打开文件
    with open(filename, 'w', encoding='utf-8') as file:
        # 将网页数据写入文件
        file.write(response.text)
    print(f"网页已保存为 {filename}")
else:
    print(f"请求失败，状态码: {response.status_code}")



